using UnityEngine;
using UnityEngine.EventSystems;

namespace LS.SimpleInput
{
    public abstract class Inputer
    {
        protected UserInputSetting m_Setting;

        /// <summary>
        /// 通常为输入设备的DeviceId，如果是记录触摸屏每根手指的输入，则为每根手指的TouchId
        /// </summary>
        public abstract int InputId { get; }
        public abstract int DeviceId { get; }
        public abstract InputPhase Phase { get; }
        public abstract float StartTime { get; }
        public abstract float DeltaTime { get; }
        public abstract float LastTime { get; }
        public abstract float Duration { get; }

        public bool Active
        {
            get
            {
                return Phase != InputPhase.None;
            }
        }

        public virtual bool Effective
        {
            get
            {
                return Phase != InputPhase.None && Phase != InputPhase.Canceled;
            }
        }

        public bool BeganThisFrame
        {
            get
            {
                return Phase == InputPhase.Began;
            }
        }

        public bool CancledThisFrame
        {
            get
            {
                return Phase == InputPhase.Canceled;
            }
        }

        public bool EndThisFrame
        {
            get
            {
                return Phase == InputPhase.Ended;
            }
        }
        public bool Moving
        {
            get
            {
                return Phase == InputPhase.Moved;
            }
        }

        public bool Stationary
        {
            get
            {
                return Phase == InputPhase.Stationary;
            }
        }

        public UserInputSetting Setting
        {
            get
            {
                return m_Setting;
            }
        }

        public Inputer(UserInputSetting setting)
        {
            if (setting == null)
                m_Setting = UserInputSetting.defaultSetting;
            else
                m_Setting = setting;
        }

        public Inputer() : this(null)
        {

        }

        public abstract void Update();

        public virtual Gesture CreateGesture()
        {
            Gesture ges = new Gesture()
            {
                inputId = InputId,
                deviceId = DeviceId,
                phase = Phase,
                gestureType = GestureType.None,
                eventType = EventType.None,
                moveDirection = MoveDirection.None,
                startTime = StartTime,
                deltaTime = DeltaTime,
                lastTime = LastTime,
                duration = Duration,
                eventSystemDetected = false,
                isTriggerThisFrame = false,
                startPoint = Vector2.zero,
                point = Vector2.zero,
                deltaPoint = Vector2.zero
            };
            return ges;
        }
    }
}
